package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.rmi.registry.Registry;
import java.sql.SQLException;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import database.JDBC;

import questionmanager.GUIQuestionManager;
import studentmanager.GUIStudentManager;

import exammanager.GUIExamManager;
import examstarter.GUIStarter;

public class GUIHome extends GUIMain{
	
	private static final long serialVersionUID = 1L;
	private JFrame myFrame;
	private GUIHomeIcon testCreating;
	private GUIHomeIcon questionManager;
	private GUIHomeIcon studentManager;
	private GUIHomeIcon examManager;
	private Registry reg;
	private boolean wanttocancel = false;
	private JButton btPort;
	
	public GUIHome(JFrame param, int cornerIndex) {
		super(param, cornerIndex);
		this.myFrame = param;
		this.setTitle("MÀN HÌNH CHÍNH");
		this.bottomPanel.setLayout(new BorderLayout());
		this.bottomPanel.setOpaque(false);
		JPanel ControlPanel, PortPanel;
		ControlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		ControlPanel.setOpaque(false);
		PortPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		PortPanel.setOpaque(false);
		testCreating = new GUIHomeIcon( 160, 200);
		testCreating.setIcon(new ImageIcon("Icons/start.png"));
		testCreating.setTitle("Tiến hành thi");
		questionManager = new GUIHomeIcon( 160, 200);
		questionManager.setIcon(new ImageIcon("Icons/question.png"));
		questionManager.setTitle("Q.Lý Câu hỏi");
		studentManager = new GUIHomeIcon( 160, 200);
		studentManager.setIcon(new ImageIcon("Icons/student.png"));
		studentManager.setTitle("Q.Lý Thí sinh");
		examManager = new GUIHomeIcon( 160, 200);
		examManager.setIcon(new ImageIcon("Icons/exam.png"));
		examManager.setTitle("Q.Lý Đề thi");
		testCreating.addMouseListener(new MouseListener(this));
		questionManager.addMouseListener(new MouseListener(this));
		studentManager.addMouseListener(new MouseListener(this));
		examManager.addMouseListener(new MouseListener(this));
		
		btPort = new CustomJButton("!", 40, 40, Color.WHITE, CustomJButton.DARKER, 
				Color.DARK_GRAY, 2, true, true);
		btPort.setFont(btPort.getFont().deriveFont(20f));
		btPort.addActionListener(new PortAction());
		
		ControlPanel.add(testCreating);
		ControlPanel.add(questionManager);
		ControlPanel.add(examManager);
		ControlPanel.add(studentManager);
		PortPanel.add(btPort);
		
		this.bottomPanel.add(ControlPanel, BorderLayout.CENTER);
		this.bottomPanel.add(PortPanel, BorderLayout.SOUTH);
		createDatabase();
	}
	
	public Registry getRegistry(){
		return reg;
	}
	
	public void setRegistry(Registry reg){
		this.reg = reg;
	}
	
	public void setCancel(boolean isCancel){
		this.wanttocancel = isCancel;
	}
	
	private boolean createDatabase(){
		File dbFile = new File("dbConfig.dbz");
		String server,dbName,user,pass;
		if(dbFile.exists()){
			try {
				DataInputStream is = new DataInputStream(new FileInputStream(dbFile));
				server = is.readUTF();
				dbName = is.readUTF();
				user = is.readUTF();
				pass = is.readUTF();
				is.close();
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "Có lỗi khi đọc tập tin dbConfig.dbz",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				GUIDBConfig dbConfig = new GUIDBConfig(myFrame,this);
				dbConfig.addWindowListener(new JDialogWindowListener());
				dbConfig.pack();
				dbConfig.setLocationRelativeTo(myFrame);
				dbConfig.setVisible(true);
				return false;
			}
			
		}else{
			JOptionPane.showMessageDialog(myFrame, "Không có cấu hình CSDL nào được tìm thấy.",
					"Thông báo", JOptionPane.WARNING_MESSAGE);
			GUIDBConfig dbConfig = new GUIDBConfig(myFrame,this);
			dbConfig.addWindowListener(new JDialogWindowListener());
			dbConfig.pack();
			dbConfig.setLocationRelativeTo(myFrame);
			dbConfig.setVisible(true);
			return false;
		}
		
		try {
			GUIMain.setDb(new JDBC(server,dbName,user,pass));
			return true;
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Có lỗi trong quá trình kết nối cới CSDL.\n", 
					"Thông báo", JOptionPane.ERROR_MESSAGE);
			GUIDBConfig dbConfig = new GUIDBConfig(myFrame,this);
			dbConfig.addWindowListener(new JDialogWindowListener());
			dbConfig.pack();
			dbConfig.setLocationRelativeTo(myFrame);
			dbConfig.setVisible(true);
			return false;
		}
	}
	
	class JDialogWindowListener extends WindowAdapter{

		@Override
		public void windowClosing(WindowEvent arg0) {
			super.windowClosing(arg0);
		}

		@Override
		public void windowClosed(WindowEvent arg0) {
			super.windowClosed(arg0);
			if(GUIMain.getDb() == null){
				if(wanttocancel){
					System.exit(-1);
				}
				createDatabase();
			}
		}	
	}
	
	class PortAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(reg != null){
				JOptionPane.showMessageDialog(parent, "Chương trình đã đăng kí port trong lần hoạt động này. \nMọi cấu hình port sẽ có tác dụng trong lần khởi động sau.",
						"Thông báo", JOptionPane.WARNING_MESSAGE);
			}
			GUIPort port = new GUIPort(myFrame);
			port.addWindowListener(new JDialogWindowListener());
			port.pack();
			port.setSize(300, port.getSize().height);
			port.setLocationRelativeTo(myFrame);
			port.setVisible(true);
		}
	}
	
	class MouseListener extends MouseAdapter{
		private GUIMain ex;
		public MouseListener(GUIMain e) {
			this.ex = e;
		}
		
		@Override
		public void mouseExited(MouseEvent arg0) {
			super.mouseExited(arg0);
			boolean isMovedOn = ((GUIHomeIcon)arg0.getSource()).isMovedOn();
			if(isMovedOn){
				((GUIHomeIcon)arg0.getSource()).setMovedOn(false);
				((GUIHomeIcon)arg0.getSource()).repaint();
			}
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			super.mouseEntered(arg0);
			boolean isMovedOn = ((GUIHomeIcon)arg0.getSource()).isMovedOn();
			if(!isMovedOn){
				((GUIHomeIcon)arg0.getSource()).setMovedOn(true);
				((GUIHomeIcon)arg0.getSource()).repaint();
			}
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			super.mousePressed(arg0);
			boolean isClicked = ((GUIHomeIcon)arg0.getSource()).isClicked();
			if(!isClicked){
				((GUIHomeIcon)arg0.getSource()).setClicked(true);
				((GUIHomeIcon)arg0.getSource()).repaint();
			}
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			super.mouseReleased(arg0);
			boolean isClicked = ((GUIHomeIcon)arg0.getSource()).isClicked();
			if(isClicked){
				((GUIHomeIcon)arg0.getSource()).setClicked(false);
				((GUIHomeIcon)arg0.getSource()).repaint();
				if(((GUIHomeIcon)arg0.getSource()).equals(questionManager)){
					JFrame guiQuestion = new CustomJFrame();
					JPanel guiQuestionPanel = new GUIQuestionManager(guiQuestion,myFrame, this.ex, 20);
					guiQuestion.add(guiQuestionPanel);
					guiQuestion.setSize(parent.getWidth(),parent.getHeight());
					guiQuestion.setLocation(parent.getLocation());
					setVisible(false);
					guiQuestion.setVisible(true);
				}
				if(((GUIHomeIcon)arg0.getSource()).equals(examManager)){
					JFrame guiExam = new CustomJFrame();
					JPanel guiExamManager = new GUIExamManager(guiExam,myFrame, this.ex, 20);
					guiExam.add(guiExamManager);
					guiExam.setSize(parent.getWidth(),parent.getHeight());
					guiExam.setLocation(parent.getLocation());
					setVisible(false);
					guiExam.setVisible(true);
				}
				if(((GUIHomeIcon)arg0.getSource()).equals(studentManager)){
					JFrame guiFrame = new CustomJFrame();
					JPanel guiStudentManager = new GUIStudentManager(guiFrame,myFrame, this.ex, 20);
					guiFrame.add(guiStudentManager);
					guiFrame.setSize(parent.getWidth(),parent.getHeight());
					guiFrame.setLocation(parent.getLocation());
					setVisible(false);
					guiFrame.setVisible(true);
				}
				
				if(((GUIHomeIcon)arg0.getSource()).equals(testCreating)){
					JFrame guiFrame = new CustomJFrame();
					JPanel guiExamStarter = new GUIStarter(guiFrame,myFrame, GUIHome.this, 20);
					guiFrame.add(guiExamStarter);
					guiFrame.setSize(parent.getWidth(),parent.getHeight());
					guiFrame.setLocation(parent.getLocation());
					setVisible(false);
					guiFrame.setVisible(true);
				}
			}
		}
	}
}
